package util.bio.xamer;
import java.util.*;
import java.text.*;

/**
 * Xamer counting related to SELEX.
 *
 */
public class Xamer {
	//fields
	private ArrayList selexOligos = new ArrayList();
	private String xamer;
	private int[] hitsPerRound;	
	private double[] percentHitsPerRound;
	private Xamer revCompRef;
	private boolean printMe = true;
	
	/**Run pairwise comparisons between percentHitsPerRound
		if any round is less than the others return true
	public boolean isEnriched(){ 
		int tester =0;
		int len = percentHitsPerRound.length;
		for (int i=0; i<len; i++){
			int tester = percentHitsPerRound
			if ()
		}
	}*/
	public String toStringBarGraph(int[] totals){
		if (printMe==false) return "";
		revCompRef.printMe=false;
		if (revCompRef.percentHitsPerRound==null) revCompRef.calculatePercentHitsPerRound(totals);
		if (percentHitsPerRound==null) calculatePercentHitsPerRound(totals);
		StringBuffer sb = new StringBuffer();
		int len = percentHitsPerRound.length;
		sb.append(xamer+"/"+revCompRef.xamer);
		for (int i=0; i<len; i++){
			sb.append("\t"+percentHitsPerRound[i]+
				"\t"+revCompRef.percentHitsPerRound[i]+"\t\t");
		}
		
		//sb.append(" Rnd"+(4)+"\t"+percentHitsPerRound[4]+"\t"+revCompRef.percentHitsPerRound[4]);
				
		sb.append("\n");
		return sb.toString();
	}
	
	public String toStringPercent(int[] totals){
		//number formatter
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(1);
		nf.setMinimumFractionDigits(2);
		
		//other revCompRef stuff
		if (revCompRef.percentHitsPerRound==null) revCompRef.calculatePercentHitsPerRound(totals);
		double[] otherPercentHitsPerRound = revCompRef.percentHitsPerRound;
		int[] otherHitsPerRound = revCompRef.hitsPerRound;
		
		if (percentHitsPerRound==null) calculatePercentHitsPerRound(totals);
		
		StringBuffer sb = new StringBuffer();
		StringBuffer sbOther = new StringBuffer();
		StringBuffer sbTotals = new StringBuffer();
		
		sb.append(nf.format(percentHitsPerRound[0])+ "("+hitsPerRound[0]+")");
		sbOther.append(nf.format(otherPercentHitsPerRound[0])+ "("+otherHitsPerRound[0]+")");
		sbTotals.append(nf.format(percentHitsPerRound[0]+otherPercentHitsPerRound[0])+
			"("+(hitsPerRound[0]+otherHitsPerRound[0])+")");
		int len = hitsPerRound.length;
		for (int i=1; i<len; i++){
			sb.append(" \t"+ nf.format(percentHitsPerRound[i])+ "("+hitsPerRound[i]+")");
			sbOther.append(" \t"+ nf.format(otherPercentHitsPerRound[i])+ "("+otherHitsPerRound[i]+")");
			sbTotals.append(" \t"+ nf.format(percentHitsPerRound[i]+otherPercentHitsPerRound[i])+
						"("+(hitsPerRound[i]+otherHitsPerRound[i])+")");
		}
		return 
		"\nXamers : "+xamer+" / "+revCompRef.getXamer()+
		"\nHPRndF : "+sb+
		"\nHPRndRC: "+sbOther+
		"\nHPRtot : "+sbTotals;
	}
		
	
	
	public void calculatePercentHitsPerRound(int[] totals){
		if (hitsPerRound==null) calculateHitsPerRound();
		percentHitsPerRound = new double[totals.length];
		for (int x= totals.length-1; x>=0; x--){
			percentHitsPerRound[x] = 100*(double)hitsPerRound[x]/(double)totals[x];
		}
	}

	public void calculateHitsPerRound(){
		int len = selexOligos.size();
		hitsPerRound = new int[len];
		for (int i=0; i<len; i++){ 
			int hits = ((ArrayList)selexOligos.get(i)).size();
			hitsPerRound[i] = hits;
		}
	}
	public int[] getHitsPerRound() {
		return hitsPerRound;
	}
	public ArrayList getSelexOligos() {
		return selexOligos;
	}
	
	public void setSelexOligos(ArrayList list) {
		selexOligos = list;
	}
	public String getXamer() {
		return xamer;
	}
	public void setXamer(String string) {
		xamer = string;
	}
	public Xamer getRevCompRef() {
		return revCompRef;
	}
	public void setRevCompRef(Xamer xamer) {
		revCompRef = xamer;
	}
	public boolean isPrintMe() {
		return printMe;
	}
	public void setPrintMe(boolean b) {
		printMe = b;
	}

}
